package com.huiquan.section.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.huiquan.framework.base.EnhancedBaseDao;
import com.huiquan.section.domain.SectionImage;
import com.ibatis.sqlmap.client.SqlMapClient;

@Repository
public class SectionImageDao extends EnhancedBaseDao<SectionImage> {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "SECTION_IMAGE.";

	public SectionImageDao() {
		super(space);
	}

	@SuppressWarnings("unchecked")
	public List<SectionImage> retrieveAllKsName() {
		Map<String, Object> param = new HashMap<>();
		return (List<SectionImage>) this.list(space + "RETRIEVE_ALL_KS_NANE", param, sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<String> retrieveAllKsClassName(Map<String, Object> param) {
		return (List<String>) this.list(space + "RETRIEVE_ALL_KS_CLASS_NANE", param, sqlMapClient);
	}
	public int retrieveSizeByName(String name, Long id) {
		Map<String, Object> param = new HashMap<>();
		param.put("name", name);
		param.put("id", id);
		Integer size = (Integer) this.object(space + "RETRIEVE_SIZE_BY_NAME", param, sqlMapClient);
		return size == null ? 0 : size;
	}

	
	public void deleteAll(){
		Map<String, Object> param = new HashMap<>();
		this.insert(space + "DELETE_ALL", param, sqlMapClient);
	}
	
	public void addBatch(List<SectionImage> list){
		this.insert(space + "INSERT_BATCH", list, sqlMapClient);
	}
	
	@SuppressWarnings("unchecked")
	public List<SectionImage> retrieveByName(String name) {
		Map<String, Object> param = new HashMap<>();
		param.put("name", name);
		return (List<SectionImage>) this.list(space + "RETRIEVE_BY_NAME", param, sqlMapClient);
	}

	/**
	 * 获取数据总数
	 * @return
	 */
	public int retrieveSizeByNameClass(String nameClass) {
		Map<String, Object> param = new HashMap<>();
		if(StringUtils.isNotBlank(nameClass)){
			param.put("nameClass", nameClass);
		}
		Integer size = (Integer) this.object(space + "RETRIEVE_SIZE_BY_NAMECLASS",param, sqlMapClient);
		return size == null ? 0 : size;
	}
	
	/**
	 * 找到比当前rank值大的且同一科室大类的第一个科室图片
	 * 
	 * @param rank
	 * @return
	 */
	public SectionImage selectFirstHighByRank(int rank,String nameClass) {
		Map<String, Object> param = new HashMap<>();
		param.put("rank", rank);
		param.put("nameClass", nameClass);
		return (SectionImage) this.object(space + "SELECT_FIRST_HIGH_BY_RANK", param, sqlMapClient);
	}

	/**
	 * 找到比当前rank值小的且同一科室大类的第一个科室图片
	 * 
	 * @param rank
	 * @return
	 */
	public SectionImage selectFirstLowByRank(int rank,String nameClass) {
		Map<String, Object> param = new HashMap<>();
		param.put("rank", rank);
		param.put("nameClass", nameClass);
		return (SectionImage) this.object(space + "SELECT_FIRST_LOW_BY_RANK", param, sqlMapClient);
	}
	
	@SuppressWarnings("unchecked")
	public List<SectionImage> retrieveByClassAndRank(String nameClass ,String  rank) {
		Map<String, Object> param = new HashMap<>();
		param.put("nameClass", nameClass);
		param.put("rank", rank);
		return (List<SectionImage>) this.list(space + "RETRIEVE_BY_CLASS_AND_RANK", param, sqlMapClient);
	}
	
	@SuppressWarnings("unchecked")
	public List<SectionImage> checkKsNameSynonymy() {
		Map<String, Object> param = new HashMap<>();
		return (List<SectionImage>) this.list(space + "CHECK_KSNAME_SYNONYMY", param, sqlMapClient);
	}
	public SectionImage retrieveObjectByIdSection(long id) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		return (SectionImage) this.object(space + "RETRIEVE_OBJECT_BY_ID_SECTION", param, sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<SectionImage> checkClassName() {
		return (List<SectionImage>) this.list(space + "CHECK_CLASS_NAME",  sqlMapClient);
	}
}
